/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package ms.jasim.model.event.impl;

import java.util.Iterator;

import ms.jasim.framework.IJasimAppContext;
import ms.jasim.model.Actor;
import ms.jasim.model.Capability;
import ms.jasim.model.Goal;
import ms.jasim.model.JasimModel;
import ms.jasim.model.event.Event;
import ms.jasim.model.event.EventPackage;
import ms.jasim.model.event.ForAllGoalDomain;
import ms.jasim.model.event.GoalSource;
import ms.jasim.model.impl.ModelObjectImpl;
import ms.jasim.model.items.Item;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.impl.ENotificationImpl;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>For All Goal Domain</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 *   <li>{@link ms.jasim.model.event.impl.ForAllGoalDomainImpl#getActor <em>Actor</em>}</li>
 *   <li>{@link ms.jasim.model.event.impl.ForAllGoalDomainImpl#getSource <em>Source</em>}</li>
 * </ul>
 * </p>
 *
 * @generated
 */
public class ForAllGoalDomainImpl extends ModelObjectImpl implements ForAllGoalDomain {
	/**
	 * The default value of the '{@link #getActor() <em>Actor</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getActor()
	 * @generated
	 * @ordered
	 */
	protected static final String ACTOR_EDEFAULT = "Actor";

	/**
	 * The cached value of the '{@link #getActor() <em>Actor</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getActor()
	 * @generated
	 * @ordered
	 */
	protected String actor = ACTOR_EDEFAULT;

	/**
	 * The default value of the '{@link #getSource() <em>Source</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getSource()
	 * @generated
	 * @ordered
	 */
	protected static final GoalSource SOURCE_EDEFAULT = GoalSource.PROVIDED_GOALS;

	/**
	 * The cached value of the '{@link #getSource() <em>Source</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getSource()
	 * @generated
	 * @ordered
	 */
	protected GoalSource source = SOURCE_EDEFAULT;

	private Iterator iterator;

	private Goal goalValue;

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected ForAllGoalDomainImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return EventPackage.Literals.FOR_ALL_GOAL_DOMAIN;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public String getActor() {
		return actor;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setActor(String newActor) {
		String oldActor = actor;
		actor = newActor;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, EventPackage.FOR_ALL_GOAL_DOMAIN__ACTOR, oldActor, actor));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public GoalSource getSource() {
		return source;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setSource(GoalSource newSource) {
		GoalSource oldSource = source;
		source = newSource == null ? SOURCE_EDEFAULT : newSource;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, EventPackage.FOR_ALL_GOAL_DOMAIN__SOURCE, oldSource, source));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public void initialize(IJasimAppContext context, Event event) {
		//IValueProvider valueProvider = context.getService(IValueProvider.class);
		Object actorObj = null; 
		Item actorItem = event.getParameters().get(getActor());
		if (actor !=null)
			actorObj = actorItem.getValue(context);
		JasimModel model = context.getService(JasimModel.class);
		if (actorObj instanceof String && model != null)
			actorObj = model.getActors().get((String)actorObj);
		if (actorObj instanceof Actor) {
			Actor a = (Actor) actorObj;
			switch (getSource()) {
			case PROVIDED_GOALS:
				iterator = a.getCapability().iterator();
				break;
			}
		}
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public boolean moveNext() {
		goalValue = null;
		while (iterator != null && goalValue == null && iterator.hasNext()) {
			Object nextObj = iterator.next();
			if (nextObj instanceof Capability)
				this.goalValue = ((Capability)nextObj).getGoal();
			
		}
		if (goalValue == null)
			this.iterator = null;
		return goalValue != null;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public void next() {
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case EventPackage.FOR_ALL_GOAL_DOMAIN__ACTOR:
				return getActor();
			case EventPackage.FOR_ALL_GOAL_DOMAIN__SOURCE:
				return getSource();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
			case EventPackage.FOR_ALL_GOAL_DOMAIN__ACTOR:
				setActor((String)newValue);
				return;
			case EventPackage.FOR_ALL_GOAL_DOMAIN__SOURCE:
				setSource((GoalSource)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case EventPackage.FOR_ALL_GOAL_DOMAIN__ACTOR:
				setActor(ACTOR_EDEFAULT);
				return;
			case EventPackage.FOR_ALL_GOAL_DOMAIN__SOURCE:
				setSource(SOURCE_EDEFAULT);
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case EventPackage.FOR_ALL_GOAL_DOMAIN__ACTOR:
				return ACTOR_EDEFAULT == null ? actor != null : !ACTOR_EDEFAULT.equals(actor);
			case EventPackage.FOR_ALL_GOAL_DOMAIN__SOURCE:
				return source != SOURCE_EDEFAULT;
		}
		return super.eIsSet(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public String toString() {
		if (eIsProxy()) return super.toString();

		StringBuffer result = new StringBuffer(super.toString());
		result.append(" (Actor: ");
		result.append(actor);
		result.append(", Source: ");
		result.append(source);
		result.append(')');
		return result.toString();
	}

	
	@Override
	public Object getValue(String name) {
		if ("Goal".equalsIgnoreCase(name))
			return goalValue;
		return null;
	}

	
	@Override
	public boolean hasValue(String name) {
		return goalValue != null && "Goal".equalsIgnoreCase(name);
	}

	
	@Override
	public void setValue(String name, Object value) {
		// TODO Auto-generated method stub
		
	}

} //ForAllGoalDomainImpl
